package com.jt.tenant.activity;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;

import com.jt.tenant.R;
import com.jt.tenant.adapter.AbnormalAdapter;
import com.jt.tenant.conn.GetAbnormalInfoList;
import com.jt.tenant.conn.GetQueryFence;
import com.jt.tenant.widget.Tools;
import com.zcx.helper.http.AsyCallBack;
import com.zcx.helper.util.UtilToast;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by AD on 2016/8/17.
 */
public class AbnoramlInfoListActivity extends JTActivity implements View.OnClickListener {

    private TextView textViewTitle;

    private ListView abnormalList;

    private LinearLayout lin_abnormal_shock,lin_abnormal_fence,lin_abnormal_plug,lin_abnormal_fault;

    private View view_shock, view_fence, view_plug, view_fault;

    private List<GetAbnormalInfoList.Abnormal> list = new ArrayList<>();

    private AbnormalAdapter adapter;

    private String type = "3";

    private GetAbnormalInfoList getAbnormalInfoList = new GetAbnormalInfoList(new AsyCallBack<GetAbnormalInfoList.Info>() {

        @Override
        public void onSuccess(String toast, int type, GetAbnormalInfoList.Info info) throws Exception {

            if(info.msg.equals("000")) {

                lin_abnormal_shock.setEnabled(true);

                lin_abnormal_fence.setEnabled(true);

                lin_abnormal_plug.setEnabled(true);

                lin_abnormal_fault.setEnabled(true);

                list.clear();

                list.addAll(info.AbnormalList);

                adapter.notifyDataSetChanged();

            }else {

                UtilToast.show(context,info.msg);

            }


        }

    });


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_abnormal_list);

        initView();

    }

    @Override
    protected void onResume() {

        super.onResume();

        switch (type){

            case "3":

                lin_abnormal_shock.performClick();

                break;

            case "1":

                lin_abnormal_fence.performClick();

                break;

            case "2":

                lin_abnormal_plug.performClick();

                break;

            case "4":

                lin_abnormal_fault.performClick();

                break;



        }


    }

    private void initView() {

        textViewTitle = (TextView) findViewById(R.id.tv_title_name);

        textViewTitle.setText("异常信息列表");

        (lin_abnormal_shock = (LinearLayout)findViewById(R.id.lin_abnormal_shock)).setOnClickListener(this);

        (lin_abnormal_fence = (LinearLayout)findViewById(R.id.lin_abnormal_fence)).setOnClickListener(this);

        (lin_abnormal_plug = (LinearLayout)findViewById(R.id.lin_abnormal_plug)).setOnClickListener(this);

        (lin_abnormal_fault = (LinearLayout)findViewById(R.id.lin_abnormal_fault)).setOnClickListener(this);

        view_shock = (View)findViewById(R.id.view_shock);

        view_fence = (View)findViewById(R.id.view_fence);

        view_plug = (View)findViewById(R.id.view_plug);

        view_fault = (View)findViewById(R.id.view_fault);

        abnormalList = (ListView) findViewById(R.id.abnormal_list);

        adapter = new AbnormalAdapter(context, list);

        abnormalList.setAdapter(adapter);

        abnormalList.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {

                switch (list.get(position).KEY) {

//                    电子围栏越界
                    case "1":

                        GetQueryFence.Fence fence = null;

                        JSONObject jsonObject = null;
                        try {
                            jsonObject = new JSONObject(list.get(position).JPUSHMESSAGE);
                        } catch (JSONException e) {
                            e.printStackTrace();
                        }

                        fence = new GetQueryFence.Fence();

                        fence.rangeNum = jsonObject.optDouble("rangeNum");

                        fence.remark = jsonObject.optString("remark");

                        fence.status = jsonObject.optString("status");

                        fence.lng = jsonObject.optDouble("lng");

                        fence.id = jsonObject.optString("id");

                        fence.outOpen = jsonObject.optString("outOpen");

                        fence.inOpen = jsonObject.optString("inOpen");

                        fence.address = jsonObject.optString("address");

                        fence.obdId = jsonObject.optString("obdId");

                        fence.name = jsonObject.optString("name");

                        fence.createTime = jsonObject.optString("createTime");

                        fence.userId = jsonObject.optString("userId");

                        fence.addressType = jsonObject.optString("addressType");

                        fence.lat = jsonObject.optDouble("lat");

                        fence.wgsLat = jsonObject.optDouble("wgsLat");

                        fence.wgsLng = jsonObject.optDouble("wgsLng");

                        fence.poiName = jsonObject.optString("poiName");

                        fence.poiAddress = jsonObject.optString("poiAddress");

                        fence.recommend_id = jsonObject.optString("recommend_id");

                        fence.dataFrom = jsonObject.optString("dataFrom");

                        fence.outStat = jsonObject.optString("outStat");

                        fence.inStat = jsonObject.optString("inStat");

                        startActivity(new Intent(context, NotifyFenceActivity.class)

                                .putExtra("Fence", fence)

                                .putExtra("isback", false)

                                .putExtra("lng", list.get(position).LON)

                                .putExtra("lat", list.get(position).LAT)
                        );

                        break;

                    //                    碰撞提醒
                    case "3":

                        startActivity(new Intent(context, ShockReportActivity.class)

                                .putExtra("create_time", Tools.getTime7(list.get(position).TIME))

                                .putExtra("value", list.get(position).MESSAGEVALUE)

                                .putExtra("lon", list.get(position).LON)

                                .putExtra("lat", list.get(position).LAT)

                                .putExtra("address", list.get(position).ADDRESS)


                        );

                        break;

                    case "4":

                        Intent i = new Intent(context, DiagnosticReportActivity.class);

                        JSONObject jsonObject1 = null;
                        try {
                            jsonObject1 = new JSONObject(list.get(position).MESSAGEVALUE);
                        } catch (JSONException e) {
                            e.printStackTrace();
                        }

                        i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TOP);

                        i.putExtra("scoreDate", jsonObject1.optString("scoreDate"))

                                .putExtra("score", jsonObject1.optString("score"))

                                .putExtra("id", jsonObject1.optString("id"))

                                .putExtra("state", jsonObject1.optString("sh"));

                        startActivity(i);

                        break;


                }

            }
        });

    }

    @Override
    public void onClick(View view) {

        super.onClick(view);

        initChoose();

        switch (view.getId()){

            case R.id.lin_abnormal_shock:

                type = "3";

                view_shock.setVisibility(View.VISIBLE);

                lin_abnormal_shock.setBackgroundResource(R.drawable.bian_bottom_c4c4c4_f5f5f5);

                getAbnormalInfoList.type = "3";

                getAbnormalInfoList.execute(context);

                break;

            case R.id.lin_abnormal_fence:

                type = "1";

                view_fence.setVisibility(View.VISIBLE);

                lin_abnormal_fence.setBackgroundResource(R.drawable.bian_bottom_c4c4c4_f5f5f5);

                getAbnormalInfoList.type = "1";

                getAbnormalInfoList.execute(context);

                break;

            case R.id.lin_abnormal_plug:

                type = "2";

                view_plug.setVisibility(View.VISIBLE);

                lin_abnormal_plug.setBackgroundResource(R.drawable.bian_bottom_c4c4c4_f5f5f5);

                getAbnormalInfoList.type = "2";

                getAbnormalInfoList.execute(context);

                break;

            case R.id.lin_abnormal_fault:

                type = "4";

                view_fault.setVisibility(View.VISIBLE);

                lin_abnormal_fault.setBackgroundResource(R.drawable.bian_bottom_c4c4c4_f5f5f5);

                getAbnormalInfoList.type = "4";

                getAbnormalInfoList.execute(context);

                break;


        }
    }

    private void initChoose(){

        lin_abnormal_fault.setEnabled(false);

        lin_abnormal_fence.setEnabled(false);

        lin_abnormal_plug.setEnabled(false);

        lin_abnormal_shock.setEnabled(false);

        lin_abnormal_fault.setBackgroundResource(R.drawable.bian_bottom_c3c3c3_eeeeee);

        lin_abnormal_fence.setBackgroundResource(R.drawable.bian_bottom_c3c3c3_eeeeee);

        lin_abnormal_plug.setBackgroundResource(R.drawable.bian_bottom_c3c3c3_eeeeee);

        lin_abnormal_shock.setBackgroundResource(R.drawable.bian_bottom_c3c3c3_eeeeee);

        view_fault.setVisibility(View.INVISIBLE);

        view_fence.setVisibility(View.INVISIBLE);

        view_plug.setVisibility(View.INVISIBLE);

        view_shock.setVisibility(View.INVISIBLE);

    }
}
